package com.pearlsoft.webapp.sysmgr.param;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.spi.ContextQueryHandler;

/**
 * 
 * @author YEHH
 *
 */
@QueryHandlerComponent(name = "SysParamLoadHandler")
public class SysParamLoadHandler extends ContextQueryHandler {
	
	protected static final Log logger = LogFactory.getLog(SysParamLoadHandler.class);
	public void handler(Query query, DataSet ds) throws Exception {
		JdbcTemplate jdbcTemplate = this.getBean("jdbcTemplate", JdbcTemplate.class);
		String sql ="SELECT * FROM SYS_PARAM ";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (Iterator<Map<String, Object>> iter = list.iterator(); iter.hasNext();) {
			Map<String, Object> map = iter.next();
			ds.newRecord();
			Iterator<?> _iterMap = map.entrySet().iterator();
			while(_iterMap.hasNext()){
				 Map.Entry<String,?> entry = (Map.Entry) _iterMap.next();
				 Object key = entry.getKey();
				 Object val = entry.getValue();
				ds.setValue(key.toString(), val);
			}
			
			
		}
	}
	
	
	public void queryOne(Query query, DataSet ds) throws Exception {
		JdbcTemplate jdbcTemplate = this.getBean("jdbcTemplate", JdbcTemplate.class);
		String id = (String) query.get("id");
		String sql = "SELECT * FROM SYS_PARAM WHERE PARAM_ID=?  ";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[] {id });
		for (Iterator<Map<String, Object>> iter = list.iterator(); iter.hasNext();) {
			Map<String, Object> map = iter.next();
			ds.newRecord();
			Iterator<?> _iterMap = map.entrySet().iterator();
			while(_iterMap.hasNext()){
				 Map.Entry<String,?> entry = (Map.Entry) _iterMap.next();
				 Object key = entry.getKey();
				 Object val = entry.getValue();
				ds.setValue(key.toString(), val);
			}
			
			
		}
	}

}
